package com.iplay.实战java高并发程序设计.chapter2;

import java.util.concurrent.locks.LockSupport;

/**
 * @author dangwu
 * @since 2021/4/29 3:51 下午
 */
public class BadSuspendToPark {
    public static final Object u = new Object();
    static ChangeObjectThread t1 = new ChangeObjectThread("t1");
    static ChangeObjectThread t2 = new ChangeObjectThread("t2");

    public static class ChangeObjectThread extends Thread{
        public ChangeObjectThread(String name){
            super.setName(name);
        }

        @Override
        public void run() {
            synchronized (u){
                System.out.println("in "+getName());
                LockSupport.park(this);
                if (Thread.interrupted()){
                    System.out.println(getName()+" 被中断了");
                }
                System.out.println(getName()+" over");
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        t1.start();
        Thread.sleep(1000);
        t2.start();
        t1.interrupt();
        LockSupport.unpark(t2);
    }
}
